﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Snake
{
    class Player
    {
        private Vector2 f_directionOfMovement = new Vector2(0,-1);
        private float f_movmentSpeed = 1.5f;
        private bool f_uppAndDown = true;
        private bool f_leftAndRight = false;
        

        private object2Dsnakebody[] f_snakeBodyPart = new object2Dsnakebody[1];
        private object2Dsnakebody f_snakeHead;

        public Player(Texture2D snakebody, Vector2 snakepos, Texture2D snakebodyLeftRigth)
        {
            f_snakeHead = new object2Dsnakebody(snakebody ,snakepos ,snakebodyLeftRigth ,f_directionOfMovement,0);
            f_snakeBodyPart[0] = f_snakeHead;

        }
        public int SnakeBodyArrayLength
        {
            get { return f_snakeBodyPart.Length; }
        }
        public Texture2D snakebody 
        {
            get {return f_snakeBodyPart[0].texture ;}
            set {f_snakeBodyPart[0].texture = value ;}
        }
        public Vector2 snakepos 
        {
            get {return f_snakeBodyPart[0].pos ;}
            set {f_snakeBodyPart[0].pos = value ;} 
        }
        public float MovementSpeed 
        {
            get {return f_movmentSpeed ;}
            set {f_movmentSpeed += value ;} 
        }
        public object2Dsnakebody GetSnakBodyPart(int index)
        {
            return f_snakeBodyPart[index];
        }
        public void Movement()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right) == true)
            {
                if (!f_leftAndRight)
                f_directionOfMovement = new Vector2(f_movmentSpeed, 0);
                f_leftAndRight = true;
                f_uppAndDown = false;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left) == true)
            {
                if (!f_leftAndRight)
                f_directionOfMovement = new Vector2(-f_movmentSpeed, 0);
                f_leftAndRight = true;
                f_uppAndDown = false;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up) == true)
            {
                if (!f_uppAndDown)
                f_directionOfMovement = new Vector2(0, -f_movmentSpeed);
                f_uppAndDown = true;
                f_leftAndRight = false;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down) == true)
            {
                if (!f_uppAndDown)
                f_directionOfMovement = new Vector2(0, f_movmentSpeed);
                f_uppAndDown = true;
                f_leftAndRight = false;
            }
            if (f_snakeBodyPart.Length > 1)
            {
                for (int i = f_snakeBodyPart.Length - 1; i > 0; i--)
                {

                    f_snakeBodyPart[i].pos = f_snakeBodyPart[i - 1].pos;
                    f_snakeBodyPart[i].diretionOfMovement = f_snakeBodyPart[i - 1].diretionOfMovement;
                }
            }
            f_snakeBodyPart[0].pos += f_directionOfMovement;
            f_snakeBodyPart[0].diretionOfMovement = f_directionOfMovement;

        }
        public void AddBody()
        {
            object2Dsnakebody Body = new object2Dsnakebody(f_snakeBodyPart[f_snakeBodyPart.Length - 1].texture, f_snakeBodyPart[f_snakeBodyPart.Length - 1].pos, f_snakeBodyPart[f_snakeBodyPart.Length - 1].texture2, f_snakeBodyPart[f_snakeBodyPart.Length - 1].diretionOfMovement,10);
            Array.Resize(ref f_snakeBodyPart,f_snakeBodyPart.Length+1);
            f_snakeBodyPart[f_snakeBodyPart.Length - 1] = Body;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            for (int i = 0; i < f_snakeBodyPart.Length; i++)
            {
            //    Vector2 drawPos = f_snakeBodyPart[i].pos;
            //    if (f_snakeBodyPart[i].diretionOfMovement.Y > 0)
            //    {
            //        drawPos += new Vector2(0, -f_snakeBodyPart[i].offset);
            //    }
            //    else if(f_snakeBodyPart[i].diretionOfMovement.Y < 0)
            //    {
            //        drawPos += new Vector2(0, f_snakeBodyPart[i].offset);
            //    }
            //    else if(f_snakeBodyPart[i].diretionOfMovement.X > 0)
            //    {
            //        drawPos += new Vector2(-f_snakeBodyPart[i].offset, 0);
            //    }
            //    else if (f_snakeBodyPart[i].diretionOfMovement.X < 0)
            //    {
            //        drawPos += new Vector2(f_snakeBodyPart[i].offset, 0);
            //    }
                    if (f_snakeBodyPart[i].diretionOfMovement.Y != 0)
                        spriteBatch.Draw(f_snakeBodyPart[i].texture, f_snakeBodyPart[i].pos, Color.White);
                    else
                        spriteBatch.Draw(f_snakeBodyPart[i].texture2, f_snakeBodyPart[i].pos, Color.White);

            }

            spriteBatch.End();
        }
        public void reset()
        {
            Array.Resize(ref f_snakeBodyPart, 1);
            Array.Resize(ref f_snakeBodyPart, 1);
            f_snakeBodyPart[0].pos = new Vector2(300,200);
            f_movmentSpeed = 1.5f;
        }
    }
}
